import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:ls_work/res/resources.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import '../util/flat_button.dart';
import 'load_image.dart';

/// 搜索页的AppBar
class SearchBar extends StatefulWidget implements PreferredSizeWidget {
  const SearchBar({
    Key? key,
    this.backgroundColor = Colors.white,
    this.hintText = "",
    this.backImg = "assets/default/ic_back_black.png",
    this.onPressed,
    this.onRightButtonClick,
    this.isShowBackButton = true,
    this.isShowRightbutton = false,
  }) : super(key: key);

  final Color backgroundColor;
  final String backImg;
  final String hintText;
  final bool isShowBackButton;
  final bool isShowRightbutton;
  final Function(String)? onPressed;
  final Function(bool)? onRightButtonClick;

  @override
  _SearchBarState createState() => _SearchBarState();

  @override
  Size get preferredSize => Size.fromHeight(48.0);
}

class _SearchBarState extends State<SearchBar> {
  SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  TextEditingController _controller = TextEditingController();
  bool _isHiddenMenu = true;

  ///照护计划  菜单隐藏显示

  Color getColor() {
    return overlayStyle == SystemUiOverlayStyle.light
        ? Colors.white
        : Colours.text_dark;
  }

  FocusNode focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    overlayStyle =
        ThemeData.estimateBrightnessForColor(widget.backgroundColor) ==
                Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle,
      child: Material(
        color: widget.backgroundColor,
        child: SafeArea(
          child: Container(
              color: widget.backgroundColor,
              child: Row(
                children: <Widget>[
                  widget.isShowBackButton == false
                      ? SizedBox(
                          width: 20,
                        )
                      : SizedBox(
                          width: 60.0,
                          height: 60.0,
                          child: InkWell(
                            onTap: () {
                              FocusScope.of(context).unfocus();
                              Navigator.maybePop(context);
                            },
                            child: Padding(
                              key: const Key('search_back'),
                              padding: const EdgeInsets.all(12.0),
                              child: Image.asset(
                                widget.backImg,
                                color: getColor(),
                              ),
                            ),
                          ),
                        ),
                  Expanded(
                    child: Container(
                      height: 40.0,
                      decoration: BoxDecoration(
                        color: Colours.bg_gray,
                        borderRadius: BorderRadius.circular(30),
                      ),
                      child: Localizations(
                        locale: const Locale("en", ""),
                        delegates: [
                          GlobalMaterialLocalizations.delegate,
                          GlobalWidgetsLocalizations.delegate,
                          GlobalCupertinoLocalizations.delegate,
                        ],
                        child: TextField(
                          key: const Key('srarch_text_field'),
                          style: TextStyles.textDark14,
                          autofocus: false,
                          controller: _controller,
                          maxLines: 1,
                          focusNode: focusNode,
                          decoration: InputDecoration(

                            border: InputBorder.none,

                            icon: Padding(
                              padding: const EdgeInsets.only(
                                  top: 8.0, bottom: 8.0, left: 8.0),
                              child: const LoadAssetImage("order_search",width: 20,height: 20,),
                            ),
                            hintText: widget.hintText,
                            hintStyle: TextStyles.textGrayC14,
                            suffixIcon: InkWell(
                              child: Padding(
                                padding: const EdgeInsets.only(
                                    left: 16.0, top: 8.0, bottom: 8.0),
                                child: const LoadAssetImage("order_delete"),
                              ),
                              onTap: () {
                                /// https://github.com/flutter/flutter/issues/35909
                                SchedulerBinding.instance
                                    .addPostFrameCallback((_) {
                                  _controller.text = "";
                                });
                              },
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  // Gaps.hGap16,
                  Theme(
                    data: Theme.of(context).copyWith(
                      buttonTheme: ButtonThemeData(
                          padding: const EdgeInsets.symmetric(horizontal: 8.0),
                          height: 32.0,
                          minWidth: 44.0,
                          materialTapTargetSize:
                              MaterialTapTargetSize.shrinkWrap,
                          // 距顶部距离为0
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(4.0),
                          )),
                    ),
                    child: widget.isShowRightbutton
                        ? Row(
                            children: [
                              FlatsButton(
                                onPressed: () {
                                  focusNode.unfocus();
                                  if (widget.onPressed != null) {
                                    widget.onPressed!(_controller.text);
                                  }
                                },
                                child: Text("搜索",
                                    style: TextStyle(
                                        fontSize: 14.0,
                                        color: Colours.app_main)),
                              ),
                              IconButton(
                                iconSize: 35,
                                icon: Icon(Icons.menu),
                                onPressed: () {
                                  _isHiddenMenu = !_isHiddenMenu;
                                  if (widget.onRightButtonClick != null) {
                                    widget.onRightButtonClick!(_isHiddenMenu);
                                  }
                                },
                              ),
                            ],
                          )
                        : TextButton(
                            onPressed: () {
                              focusNode.unfocus();
                              if (widget.onPressed != null) {
                                widget.onPressed!(_controller.text);
                              }
                            },
                            style: ButtonStyle(
                                padding:
                                    WidgetStateProperty.all(EdgeInsets.zero),
                            shape: WidgetStateProperty.all(StadiumBorder())),
                      
                            child: Text("搜索",
                                style: TextStyle(
                                    fontSize: 14.0, color: Colours.app_main)),
                          ),
                  ),
                  // Gaps.hGap16,
                ],
              )),
        ),
      ),
    );
  }
}
